<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="姓名">
                <a-input placeholder="请输入姓名" v-model="queryParam.name" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="手机号">
                <a-input placeholder="请输入手机号" v-model="queryParam.mobile" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="起止时间">
                <a-range-picker
                  format="YYYY-MM-DD HH:mm"
                  valueFormat="YYYY-MM-DD HH:mm"
                  :placeholder="['开始时间', '结束时间']"
                  v-model="queryTime"
                  @change="changeTime"
                />
              </a-form-item>
            </a-col>
          </a-row>
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="选择门店">
                <a-select v-model="queryParam.md_id">
                  <a-select-option :value="item.id" v-for="item in allShop" :key="item.id">
                    {{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="手机号">
                <a-input placeholder="请输入手机号" v-model="queryParam.mobile" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="">
                <a-button type="primary" @click="getStaffList">查询</a-button>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>
    <a-card :bordered="false">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-button @click="resetQueryParams">全部员工</a-button>
      </div>
      <a-table
        :columns="columns"
        :data-source="userList"
        :pagination="{
          total: this.totalCount,
          showSizeChanger: false,
          onChange: this.tablePageChange,
        }"
        :rowKey="
          (r, i) => {
            return r.id
          }
        "
      >
        <div slot="user" slot-scope="scope">
          <div class="name">
            <div class="pic">
              <img :src="scope.avatar" alt="" />
            </div>
            <div class="info">
              <span>{{ scope.nickname }}</span>
            </div>
          </div>
        </div>
        <span slot="created_at" slot-scope="text">
          {{ text > 0 ? $moment(text * 1000).format('YYYY-MM-DD HH:mm:ss') : '-' }}
        </span>
        <div slot="status" slot-scope="scope">
          <div class="status">
            <span :class="scope.is_disable == 0 ? 'open' : 'close'"></span>
            <span>{{ scope.is_disable == 0 ? '启用' : '禁用' }}</span>
          </div>
        </div>
        <span slot="action" slot-scope="scope">
          <a-button type="link" @click="deleteStaff(scope.id)" :disabled="scope.is_manager == 1">删除</a-button>
        </span>
      </a-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { staffList, allShop, batchStaff } from '@/api/outlet'
import { STable } from '@/components'

const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
  },
  {
    title: '员工信息',
    scopedSlots: {
      customRender: 'user',
    },
    width: 200,
  },
  {
    title: '姓名',
    dataIndex: 'name',
  },
  {
    title: '门店名称',
    dataIndex: 'md_name',
  },
  {
    title: '手机号',
    dataIndex: 'mobile',
  },
  {
    title: '加入时间',
    dataIndex: 'created_at',
    sorter: (a, b) => a.created_at - b.created_at,
    scopedSlots: {
      customRender: 'created_at',
    },
  },
  {
    title: '状态',
    scopedSlots: {
      customRender: 'status',
    },
  },
  {
    title: '操作',
    scopedSlots: {
      customRender: 'action',
    },
  },
]

export default {
  name: 'StaffList',
  components: {
    STable,
  },
  data() {
    return {
      // 表头
      columns,
      // 查询条件参数
      queryParam: {
        pageNo: 1,
        pageSize: 10,
        name: null,
        mobile: null,
        start_time: null,
        end_time: null,
        md_id: null,
      },
      // 员工列表
      userList: [],
      // 数据总条数
      totalCount: 0,
      // 所有门店列表
      allShop: [],
      // 时间间隔
      queryTime: [],
    }
  },
  created() {
    this.getAllShop()
    this.getStaffList()
  },
  methods: {
    // 重置查询参数
    resetQueryParams() {
      Object.assign(this.queryParam, {
        pageNo: 1,
        pageSize: 10,
        name: null,
        mobile: null,
        start_time: null,
        end_time: null,
        md_id: null,
      })
      this.getStaffList()
    },
    // 分页请求
    tablePageChange(e) {
      this.queryParam.pageNo = e
      this.getStaffList()
    },
    // 切换筛选时间
    changeTime(e) {
      if (e.length) {
        this.queryParam.start_time = e[0]
        this.queryParam.end_time = e[1]
      } else {
        this.queryParam.start_time = null
        this.queryParam.end_time = null
      }
    },
    // 获取所有门店列表
    async getAllShop() {
      const res = await allShop()
      if (res.code == 0) {
        this.allShop = res.data
      }
    },
    // 获取员工列表
    async getStaffList() {
      const hide = this.$message.loading('加载中', 0)
      const res = await staffList(this.queryParam)
      if (res.code == 0) {
        this.userList = res.data.data
        this.totalCount = res.data.totalCount
      }
      hide()
    },
    // 点击删除员工
    deleteStaff(e) {
      let that = this
      this.$confirm({
        title: '是否删除员工?',
        async onOk() {
          const res = await batchStaff({
            id: [e],
            type: 'is_delete',
            status: 1,
          })
          if (res.code == 0) {
            that.getStaffList()
          }
        },
        onCancel() {},
      })
    },
  },
}
</script>

<style lang="less" scoped>
.permission-form {
  /deep/ .permission-group {
    margin-top: 0;
    margin-bottom: 0;
  }
}
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  /deep/ .ant-input-affix-wrapper {
    width: 270px;
    margin-top: 14px;
    margin-right: 20px;
  }
  .select {
    display: flex;
    align-items: center;
    margin-top: 14px;
    margin-right: 20px;
  }
  .ant-btn {
    margin-top: 14px;
  }
}
.name {
  display: flex;
  align-items: center;
  .pic {
    width: 48px;
    min-width: 48px;
    height: 48px;
    border-radius: 4px;
    overflow: hidden;
    margin-right: 24px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .info {
    flex: auto;
    width: 0;
    display: flex;
    flex-direction: column;
    font-size: 14px;
    span {
      width: 100%;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    span:nth-child(2) {
      color: rgba(0, 0, 0, 0.45);
      margin-top: 6px;
    }
  }
}
.status {
  display: flex;
  align-items: center;
  span:nth-child(1) {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    margin-right: 4px;
  }
  .close {
    background-color: #f5222d;
  }
  .open {
    background-color: #65c817;
  }
}
</style>
